<template>
  <div class="me">
    <div class="headInfo">
      <div class="head-img"></div>
      <div class="head-profile">
        <p class="user-id" v-if="userInfo._id" >{{userInfo._id}}</p>
        <p v-else class="user-id" @click="handleLogin">登录/注册</p>
        <p class="user-phone">
          <i class="fa fa-mobile"></i>
          <span v-if="userInfo.phone">{{userInfo.phone | filterPhone}}</span>
          <span v-else>登录后享受更多特权</span>
        </p>
      </div>
       <i class="fa fa-angle-right" />
    </div>
    <!-- 退出登录 -->
    <div v-if="userInfo">
      <div @click="handleAddress" class="address-cell">
        <i class="fa fa-map-marker" />
        <span>我的地址</span>
        <i class="fa fa-angle-right" />
      </div>
      <van-button v-if="isShow"  @click="handleOut" size="large">退出登录</van-button>
    </div>
  </div>
</template>

<script>
import {Button} from 'vant'
export default {
  name:"Mine",
  components:{[Button.name]:Button},
  data() {
    return {
      userInfo:{},
      isShow:false
    }
  },
  activated() {
    this.getData()
    
  },
    filters: {
    // 隐藏手机号中间四位
    filterPhone(phone) {
      return phone.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
    }
  },

  methods: {
    getData() {
      const userId = localStorage.getItem("token")
      axios
        .get("/api/user/user_info/" + userId)
        .then(res => {
          console.log(res);
          this.userInfo = res;
          this.isShow=true
        })
        .catch(err => {
          console.log(err);
        });
    },
    handleLogin(){
      this.$router.push('/login')
    },
    handleAddress(){
      if(this.userInfo.myAddress.length>0){
        this.$router.push('/myaddress')
      }else{
        this.$router.push('/addaddress')
      }
    },
    // 退出
    handleOut(){
      localStorage.removeItem('token')
      this.$router.push('/login')
       this.isShow=false
    }
  },
}
</script>

<style scoped>
.me {
  overflow-x: hidden;
  overflow-y: auto;
  width: 100%;
  height: 100%;
}

.headInfo {
  display: flex;
  align-items: center;
  padding: 20px 15px;
  color: #ffffff;
  background-image: linear-gradient(90deg, #00aaff, #0085ff);
}

.head-img {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-image: url("../../assets/user.png");
  background-position: 0 0;
  background-size: 60px;
}

.head-profile {
  overflow: hidden;
  flex-grow: 1;
  margin-left: 20px;
}

.head-profile .user-id {
  font-size: 18px;
  font-weight: 700;
  display: flex;
  overflow: hidden;
  align-items: center;
  max-width: 220px;
  margin-bottom: 10px;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.head-profile .user-phone {
  font-size: 16px;
  display: flex;
  align-items: center;
}

.user-phone > i {
  font-size: 24px;
  display: inline-block;
  margin-right: 6px;
}

.headInfo > i {
  font-size: 30px;
}

.headInfo > i {
  font-size: 30px;
}

.address-cell {
  position: relative;
  display: flex;
  align-items: center;
  height: 50px;
  padding-right: 15px;
  padding-left: 15px;
  color: #666666;
  border-bottom: 1px solid #eeeeee;
  background: #ffffff;
}

.address-cell i:first-child {
  font-size: 24px;
  display: inline-block;
  margin-right: 6px;
  color: rgb(74, 165, 240);
}

.address-cell span {
  font-size: 16px;
  display: inline-block;
}

.address-cell i:last-child {
  font-size: 30px;
  position: absolute;
  right: 15px;
  display: inline-block;
  color: #999999;
}

/deep/ .van-button__text {
  color: red;
}

</style>